import requests
import json
import gevent
import time
import tools.hd_logger

logger = tools.hd_logger.HdLogger()

# 向AGV发送请求
def call_out_AGV_Reclaimer_01(timestamp, orderId):
    data1 = {
        "modelProcessCode": "xialiao2",  # 任务流程模板编码
        "priority": 6,   # 优先级，正常优先级
        "fromSystem":"HD",   # 来源系统，HD就可以
        "orderId":"HD{}".format(timestamp),  # 任务编号，自定义
        "taskOrderDetail": [
            {
                "taskPath": orderId,
            },
            {
                "taskPath": orderId,
            }
        ]
    }
    logger.debug(data1)
    headers = {'Content-Type':'application/json'}
    response = requests.post(url="http://192.168.200.55:7000/ics/taskOrder/addTask", data=json.dumps(data1), headers=headers).content
    return response

# 获取请求状态
def get_task_status(orderId):
    data2 = {
        "orderId": orderId
    }
    headers = {'Content-Type':'application/json'}
    response = requests.post(url="http://192.168.200.55:7000/ics/out/task/getTaskOrderStatus", data=json.dumps(data2), headers=headers).content
    return response

# 取消一个任务编号的请求
def set_call_off_request(timestamp):
    data3 = [
        {
            "orderId": "HD{}".format(timestamp)
        }
    ]
    logger.debug(data3)
    headers = {'Content-Type':'application/json'}
    response = requests.post(url="http://192.168.200.55:7000/ics/out/task/cancelTask", data=json.dumps(data3), headers=headers).content
    return response




if __name__ == '__main__':
    """
    1 未发送,4:正在发送，9 已下发，6 运行中，
    7 执行失败，5 发送失败，3 已取消，8 已
    完成，10 等待确认。
    """
    """
    1号托盘 20000001
    2号托盘 20000002
    """
    #
    # id = 20000002
    # timestamp = int(time.time())
    # responseCall = call_out_AGV_Reclaimer_01(timestamp, id)
    # print(json.loads(str(responseCall, 'utf8')))

    timestamp = "HD1687868401"

    # print(timestamp)
    a= get_task_status(timestamp)
    # a = set_call_off_request(timestamp)
    a = json.loads(str(a, 'utf8'))
    print(a)

